/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package farcast;

import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Jeroen
 */
public class Midlet extends MIDlet implements CommandListener, ItemCommandListener  {
    private DisplayableManager dManager;
    private Display display;
    private boolean firstStart = true;
    
    private Form mainScreen;
    private Form editTitleScreen;
    private Form editTextScreen;
    
    private StringItem txtTitle;
    private StringItem txtText;
    private TextField editField;
    
    private Vector files;
    
    private Command cmdClear;
    private Command cmdExit;
    private Command cmdSend;
    private Command cmdEdit;
    private Command cmdOk;
    private Command cmdBack;
    
    public Midlet (){
        files = new Vector();
        
        display = Display.getDisplay(this);
        dManager = new DisplayableManager();
        cmdClear = new Command("Clear", Command.OK,0);
        cmdSend = new Command("Send", Command.OK, 1);
        cmdExit = new Command("Exit", Command.EXIT, 2);  
        cmdEdit = new Command("Edit", Command.OK, 0);
        cmdOk = new Command("OK", Command.OK, 0);
        cmdBack = new Command("Back", Command.OK, 1);
    }
    
    
    public void startApp() {
        if(firstStart){
            firstStart = false;
            dManager.pushDisplayable(getMainScreen());
        }
        display.setCurrent(dManager.getCurrent());
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command c, Displayable d){
        if(c == cmdSend){
            
        }
        if(c == cmdExit){
           destroyApp(true);
           notifyDestroyed(); 
        }
        if(c == cmdClear){
            
        }
        if(c == cmdOk){
            if (d == editTitleScreen){
               txtTitle.setText(editField.getString()); 
            }
            if (d == editTextScreen){
               txtText.setText(editField.getString()); 
            }
            dManager.popDisplayable();
        }
        if(c == cmdBack){
            dManager.popDisplayable();
        }
        display.setCurrent(dManager.getCurrent());
    }
    
    public void commandAction(Command c, Item item){
        if(c == cmdEdit){
            if(item == txtTitle){
                dManager.pushDisplayable(editTitle());
            }
            if(item == txtText){
                dManager.pushDisplayable(editText());
            }
            display.setCurrent(dManager.getCurrent());
        }
    }
    
    private Form getMainScreen(){
        if(mainScreen == null){
            mainScreen = new Form("StartScreen");
            txtTitle = new StringItem("Title", "");
            txtText= new StringItem("Text", "");
            mainScreen.append(txtText);
            mainScreen.append(txtTitle);
            mainScreen.addCommand(cmdClear);
            mainScreen.addCommand(cmdSend);
            mainScreen.addCommand(cmdExit);
            txtTitle.addCommand(cmdEdit);
            txtTitle.setItemCommandListener(this);
            txtText.addCommand(cmdEdit);
            txtText.setItemCommandListener(this);
            mainScreen.setCommandListener(this);
        }
        return mainScreen;
    }
    
    private Form editTitle(){
        if(editTitleScreen == null){
            System.out.println("Create EditTitle");
            editTitleScreen = new Form("Edit Title");
            editTitleScreen.addCommand(cmdOk);
            editTitleScreen.addCommand(cmdBack);
            editField = new TextField("Title",txtTitle.getText(),80,TextField.ANY);
            editTitleScreen.append(editField);
            editTitleScreen.setCommandListener(this);
        }
        return editTitleScreen;
    }
    
    private Form editText(){
         if(editTextScreen == null){
            editTextScreen = new Form("Edit Text");
            editTextScreen.addCommand(cmdOk);
            editTextScreen.addCommand(cmdBack);
            editField = new TextField("Text",txtText.getText(),500,TextField.ANY);
            editTextScreen.append(editField);
            editTextScreen.setCommandListener(this);
        }
        return editTextScreen;
    }
}
